package com.example.meijun.pantest;


import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;

/***
 * 参考okhttp的官方demo，此类当中我们主要把注意力放在ProgressListener和read方法中。
 * 在这里获取文件总长我写在了构造方法里，这样免得在source的read方法中重复调用或判断。
 * creade by meijun 2017-4-6 11:22
 *
 */
public class ProgressResponseBody extends ResponseBody {

    public interface ProgressListener {
        /**
         *
         * @param contentLength 服务器返回的文件大小
         *
         */
        void onPreExecute(long contentLength);
        void update(long totalBytes, boolean done);

        void cancle();
    }

    private final ResponseBody responseBody;
    private final ProgressListener progressListener;
    private BufferedSource bufferedSource;


    private File file;
    public ProgressResponseBody(ResponseBody responseBody,
                                ProgressListener progressListener, File destination) throws IOException {
        this.responseBody = responseBody;
        this.progressListener = progressListener;
        this.file = destination;
        if(progressListener!=null ){

            progressListener.onPreExecute(contentLength() + destination.length());
        }
    }

    @Override
    public MediaType contentType() {
        return responseBody.contentType();
    }

    @Override
    public long contentLength() {
        return responseBody.contentLength();
    }

    @Override
    public BufferedSource source() {
        if (bufferedSource == null) {
            bufferedSource = Okio.buffer(source(responseBody.source()));
        }
        return bufferedSource;
    }

    private Source source(Source source) {
        return new ForwardingSource(source) {
            long totalBytes = 0L;
            @Override
            public long read(Buffer sink, long byteCount) throws IOException {
                long bytesRead = super.read(sink, byteCount);
                // read() returns the number of bytes read, or -1 if this source is exhausted.
                totalBytes += bytesRead != -1 ? bytesRead : 0;
                if (null != progressListener) {
                    progressListener.update(file.length(), bytesRead == -1);
                }
                return bytesRead;
            }
        };
    }
}